package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.UserAddressEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.AddressAddActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 */
public class AddressAddModel extends BaseViewModel {

    public ObservableField<UserAddressEntity> userAddressEntity = new ObservableField<>(new UserAddressEntity());
    private AddressAddActivity activity;
    private SelfRepertory repertory;

    public AddressAddModel(AddressAddActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /**
     * 所有地址选项
     */
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                activity.initAddressData(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取当前id的地址信息
     */
    public void getIdAddress(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().searchAddress(id).enqueue(new Callback<BaseEntity<UserAddressEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserAddressEntity>> call, Response<BaseEntity<UserAddressEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState().equals("ok")) {
                    activity.showAddress(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserAddressEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 提交新的收货地址
     */
    public void commitAddress() {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().addAddress(userAddressEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    MToast.showToast(activity, response.body().getMsg());
                    if (response.body().getState().equals("ok")) {
                        activity.finish();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 修改当前id的收货地址
     */
    public void overrideAddress() {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().editAddress(userAddressEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    MToast.showToast(activity, response.body().getMsg());
                    if (response.body().getState().equals("ok")) {
                        activity.finish();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void delIdAddress(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().delAddress(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity, response.body().getData().toString());
                    activity.finish();

                }
                assert response.body() != null;
                MToast.showToast(activity, response.body().getMsg());

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
